package com.li.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.li.constant.MessageConstant;
import com.li.entity.PageResult;
import com.li.entity.QueryPageBean;
import com.li.entity.Result;
import com.li.pojo.Setmeal;
import com.li.service.SetmealService;
import com.li.utils.QiniuUtils;
import com.li.utils.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    SetmealService setmealService;
    @Autowired
    JedisPool jedisPool;
    /*
     定义文件上传控制器
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile){

        try {
            //获取文件名
            String originalFilename = imgFile.getOriginalFilename();
            //获取文件后缀
            int lastIndexOf = originalFilename.lastIndexOf(".");
            String suffix = originalFilename.substring(lastIndexOf);
            //使用uuid生成随机名
            String filename = UUID.randomUUID().toString() + suffix;
            //开始上传
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), filename);
            //图片上传成功
            jedisPool.getResource().sadd(RedisConstant.getSetmealPicResources(),filename);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,filename);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }

    }

    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){

        try {
            setmealService.add(setmeal, checkgroupIds);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        return setmealService.findPage(queryPageBean);
    }


    // 删除套餐信息
    @RequestMapping("/deleteById")
    public Result deleteById(int id, String img) {
        try {

            setmealService.deleteById(id);
            setmealService.deleteByImgInRedis(img);
            return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);

        }
    }

    // 编辑前回显数据
    @RequestMapping("/findSetmeals")
    public Result findSetmeals(int id) {
        Setmeal setmeal = setmealService.findSetmeal(id);

        return new Result(true, "", setmeal);
    }

    @RequestMapping("/findCheckGroupsIds")
    public Result findCheckGroupsIds(int id) {
        Integer[] checkgroupIds = setmealService.findCheckGroupsIdsById(id);
        return new Result(true, "", checkgroupIds);

    }


    // 修改数据方法
    @RequestMapping("/updateSetMeal")
    public Result updateSetMeal(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {

        try {
            setmealService.update(setmeal, checkgroupIds);
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);

        }
    }


}
